/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unplag.core;
import java.util.*;
import java.io.*;
/**
 *
 * @author Naruil
 */
public class Work {

    private File file;
    private BaseDir base;
    private Map<TargetFile,String> buf=new HashMap<TargetFile,String>();
    protected Work(BaseDir base,File file){
        this.file=file;
        this.base=base;
    }

    public BaseDir getBaseDir(){
        return base;
    }

    public String getName(){
        return file.getName();
    }

    protected File getFile(){
        return file;
    }

    @Override
    public String toString(){
        return "Work:"+getName();
    }

    public String getFileContent(TargetFile target){
        String content=buf.get(target);
        if(content==null){
            content=read(target.getFilename());
            buf.put(target, content);
        }
        return content;
    }

    private String read(String fileName){
        FileReader fr=null;
        try {
            fr = new FileReader(this.getFile().getPath()+"\\"+fileName);
            BufferedReader br = new BufferedReader(fr);
            StringBuilder sb=new StringBuilder();
            String line;
            while((line=br.readLine())!=null){
                sb.append(line);
                sb.append("\n");
            }
            fr.close();
            return sb.toString();
        } catch (IOException ex) {
        } finally {
            try {
                fr.close();
            } catch (Exception ex) {
            }
        }
        return null;
    }
}
